import UnitBezier from '@mapbox/unitbezier';

export function bindAll(methods: string[], context: any) {
  for (let i = 0; i < methods.length; i += 1) {
    context[methods[i]] = context[methods[i]].bind(context);
  }
}

export function clamp(num: number, min: number, max: number) {
  if (num < min) {
    return min;
  }
  if (num > max) {
    return max;
  }
  return num;
}

/**
 * Given given (x, y), (x1, y1) control points for a bezier curve,
 * return a function that interpolates along that curve.
 *
 * @param p1x control point 1 x coordinate
 * @param p1y control point 1 y coordinate
 * @param p2x control point 2 x coordinate
 * @param p2y control point 2 y coordinate
 * @private
 */
export function bezier(p1x: number, p1y: number, p2x: number, p2y: number): (t: number) => number {
  const unitBezier = new UnitBezier(p1x, p1y, p2x, p2y);
  return (t: number) => unitBezier.solve(t);
}
